using System;
using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class MultiplayerRooms : ScriptableObject, MatchEventsDelegate
{
	[Serializable]
	public class KFactorRange
	{
		public float minRating;

		public float maxRating = 10000f;

		public int priority;

		public bool isDefault;

		public float k = 32f;

		public bool useMultiplayerMatchesRange;

		public int minMatchesPlayed;

		public int maxMatchesPlayed;

		public bool CanUse(float rating, int numberOfMatchesPlayed)
		{
			if (minRating > rating || rating > maxRating)
			{
				return false;
			}
			if (useMultiplayerMatchesRange && (minMatchesPlayed > numberOfMatchesPlayed || numberOfMatchesPlayed > maxMatchesPlayed))
			{
				return false;
			}
			return true;
		}
	}

	[Serializable]
	public class RoomInfo
	{
		public string roomSprite;

		public bool useRibbonSprite = true;

		public string ribbonSprite;

		public Color ribbonColor = Color.white;

		public string coinSprite;

		public int fee;

		public int prize;

		public MultiplayerRoomType room;

		public string roomName;

		public float score;

		public int EloRatingK;

		public string overrideWinMsgOnCard;

		public string overrideFeeMsgOnCard;

		public string arenaName;

		public int maxStarRankForEntry;

		public float influenceOfSkill;

		public float skillOffset;

		public float inflounceOfEquipment;

		public bool shouldReportToServer;

		public bool shouldShowIntro;

		[Range(0f, 1f)]
		public float racketInfluence;

		[Range(0f, 1f)]
		public float shoeInfluence;

		public FloatRange maxSkillDifference = new FloatRange
		{
			min = -1000f,
			max = 1000f
		};
	}

	protected static MultiplayerRooms instance_;

	public List<RoomInfo> rooms = new List<RoomInfo>();

	public CurrencyType multiplayerCurrencyType;

	public List<KFactorRange> kFactorRanges = new List<KFactorRange>();

	public static MultiplayerRooms instance
	{
		get
		{
			if (instance_ == null)
			{
				instance_ = Resources.Load("MultiplayerRooms", typeof(MultiplayerRooms)) as MultiplayerRooms;
			}
			return instance_;
		}
	}

	public RoomInfo FindRoomByType(MultiplayerRoomType room)
	{
		foreach (RoomInfo room2 in rooms)
		{
			if (room2.room == room)
			{
				return room2;
			}
		}
		return null;
	}

	public void StartMatch(TableTennisMatchController match)
	{
		RoomInfo roomInfo = FindRoomByType(MatchParameters.InitParameters.multiplayerParams.roomType);
		if (roomInfo != null && roomInfo.fee > 0)
		{
			GGPlayerSettings.instance.walletManager.BuyItem(roomInfo.fee, multiplayerCurrencyType);
		}
	}

	public void MatchComplete(TableTennisMatchController match)
	{
		Debug.Log("MATCH COMPLETE");
		TableTennisClassic tableTennisClassic = match.gameType as TableTennisClassic;
		if (!(tableTennisClassic != null))
		{
			return;
		}
		match.gameDone = true;
		match.ui.pauseButton.SetActive(false);
		TableTennisPlayerBase playerThatWon = tableTennisClassic.playerThatWon;
		bool flag = playerThatWon.teamTag == 0;
		bool shouldUpdateLeaderboards = true;
		MatchParameters.MatchParams.MultiplayerParams multiplayerParams = MatchParameters.InitParameters.multiplayerParams;
		if (MIntro.HasPidInIntro(MatchParameters.InitParameters.multiplayerParams.multiplayerVersion) && MatchParameters.InitParameters.multiplayerParams.player1Pid == MatchParameters.InitParameters.multiplayerParams.player2Pid && !string.IsNullOrEmpty(MatchParameters.InitParameters.multiplayerParams.player2Pid))
		{
			GGDebug.Log("Same PID");
			match.OnGameWon(flag);
			return;
		}
		float eloRank = MatchParameters.InitParameters.multiplayerParams.eloRank;
		GGPlayerSettings.instance.UpdateEloRating(flag ? 1 : 0, eloRank);
		RoomInfo roomInfo = FindRoomByType(MatchParameters.InitParameters.multiplayerParams.roomType);
		if (roomInfo != null && roomInfo.fee > 0 && flag)
		{
			GGPlayerSettings.instance.walletManager.AddCurrency(multiplayerCurrencyType, roomInfo.prize);
		}
		if (roomInfo != null && roomInfo.fee > 0 && MIntro.HasPidInIntro(MatchParameters.InitParameters.multiplayerParams.multiplayerVersion))
		{
			PlayedGamesManager playedGamesManager = SingletonInit<PlayedGamesManager>.instance;
			string player2Pid = MatchParameters.InitParameters.multiplayerParams.player2Pid;
			int roomType = (int)MatchParameters.InitParameters.multiplayerParams.roomType;
			PlayedGamesTracker.PlayedGame gameForOpponentId = playedGamesManager.GetGameForOpponentId(player2Pid);
			if (gameForOpponentId == null || !playedGamesManager.IsTimeOk(gameForOpponentId))
			{
				playedGamesManager.OnGamePlayed(player2Pid, roomType);
			}
			else
			{
				shouldUpdateLeaderboards = false;
			}
		}
		TableTennisMultiplayerMatchController tableTennisMultiplayerMatchController = match as TableTennisMultiplayerMatchController;
		if (tableTennisMultiplayerMatchController != null)
		{
			tableTennisMultiplayerMatchController.OnMultiplayerGameWon(flag, shouldUpdateLeaderboards);
		}
		else
		{
			match.OnGameWon(flag);
		}
	}

	public KFactorRange GetKFactorRange(float rating, int numberOfMatchesPlayed)
	{
		KFactorRange kFactorRange = null;
		foreach (KFactorRange kFactorRange2 in kFactorRanges)
		{
			if (kFactorRange2.CanUse(rating, numberOfMatchesPlayed) && (kFactorRange == null || kFactorRange2.priority > kFactorRange.priority))
			{
				kFactorRange = kFactorRange2;
			}
		}
		if (kFactorRange == null)
		{
			foreach (KFactorRange kFactorRange3 in kFactorRanges)
			{
				if (kFactorRange3.isDefault)
				{
					return kFactorRange3;
				}
			}
			if (kFactorRanges.Count > 0)
			{
				return kFactorRanges[0];
			}
			return null;
		}
		return kFactorRange;
	}
}
